

*************************************************************
************ PREPARE BORROWER FILE
*************************************************************

// defines borrowers as GCEL parents
// creates link files between main borrower identifier and the IJ Global and Dealscan company identifiers

//get Dealscan borrowers linked to GCEL
use ../Raw/DealScan/DS_newIDs_GCEL_link, clear
ren Borrower_Id CompanyID_DS
drop if missing(GCELcompanyID)
duplicates report CompanyID_DS
//we aggregate to GCEL parent ID. 
keep CompanyID_DS Company_DS ParentCompany_GCEL ParentID_GCEL 
duplicates drop 
duplicates report CompanyID_DS //some dealscan companies match to more than one parent small number, represents joint ownership.
tempfile ds_gcel_list
save `ds_gcel_list'
keep ParentID_GCEL
duplicates drop 
tempfile ds_gcel_ids 
save `ds_gcel_ids'

//gen IJ global borrowers linked to GCEL 
use ../Raw/IJGlobal/gcel_ijglobal_link, clear
drop if missing(GCELcompanyID)

//aggreagte to GCEL parent ID 
keep TransactionId Borrower_CompanyName Borrower_ParentCompany ParentCompany_GCEL GCELParentId 
rename GCELParentId ParentID_GCEL
duplicates drop 
drop TransactionId
duplicates drop
duplicates report Borrower_CompanyName
rename Borrower_CompanyName Borrower_CompanyName_IJG 
rename Borrower_ParentCompany Borrower_ParentCompany_IJG

replace Borrower_CompanyName_IJG = "China Gezhouba Group Corporation" if Borrower_CompanyName_IJG=="China Gezhouba Group Corporation "
replace Borrower_CompanyName_IJG = "China Energy Engineering Group" if Borrower_CompanyName_IJG=="China Energy Engineering Group "
replace Borrower_CompanyName_IJG = "Universal Coal" if Borrower_CompanyName_IJG=="Universal Coal "

//Some IJ Global companies match to more than one GCEL parent with small number, represents joint ownership.
tempfile ijg_gcel_list
save `ijg_gcel_list'
keep ParentID_GCEL
duplicates drop 
tempfile ijg_gcel_ids
save `ijg_gcel_ids'

//LOAD PARENT GCEL INFO 
use ../Intermediate/GCEL_parent_clean, clear 
//merge in lists of GCEL parent IDs from IJ Global and Dealscan borrowers
merge 1:1 ParentID_GCEL using `ds_gcel_ids', keep(1 3)
gen has_ds_matches = _merge == 3
drop _merge
merge 1:1 ParentID_GCEL using `ijg_gcel_ids', keep(1 3)
gen has_ijg_matches = _merge == 3
drop _merge

gen borrower_id = ParentID_GCEL
order borrower_id
rename ParentCompany borrower_name
order borrower_name, after(borrower_id)
save ../Intermediate/borrower_file, replace


//Now get link from underlying datas to the borower ID in question 
use `ds_gcel_list', clear
gen borrower_id = ParentID_GCEL 
order borrower_id 
drop ParentID_GCEL
rename ParentCompany_GCEL borrower_name
order borrower_name, after(borrower_id)
drop Company_DS
compress 


save ../Intermediate/dealscan_borrowerid_map, replace

use `ijg_gcel_list', clear
gen borrower_id = ParentID_GCEL 
order borrower_id 
drop ParentID_GCEL
rename ParentCompany_GCEL borrower_name
order borrower_name, after(borrower_id)
compress 
save ../Intermediate/ijglobal_borrowerid_map, replace



